<?php

class InvestController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules(){
	    return array(
	        array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
	        array('deny', // deny anything else
	              'users'=>array('*'),
	        ),
	    );
	}

    public function actionIndex()
    {   
            $this -> render('index',array(	

            ));
    }

    public function actionSearch()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $invest = new Invest();

            $data = $invest -> search();

            echo json_encode(array("results" => $data[0], "total" => $data[1]));
            return;
        }
    }

    public function actionCreate()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $invest = new Invest();
            $invest -> hyipId = $_POST['domain'];
            $invest -> deposit = $_POST['deposit'];
            $invest -> planWithdraw = $_POST['planWithdraw'];
            $invest -> profitDaily = $_POST['profitDaily'];
            $invest -> duration = $_POST['duration'];

            $format = "Y-m-d H:i";
            if(!empty($_POST['investedDate']))
            {
                $invest -> investedDate = date($format, strtotime(str_replace("/", "-", $_POST['investedDate'])));
            }
            
            if($invest -> save())
            {
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }

            return;
        }
    }
    
    public function actionDelete()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $item = Invest::model() -> findByPk($_POST['id']);
             if ($item -> delete()) {
                echo json_encode(array("status" => 'success'));
            } else {
                echo json_encode(array("status" => 'failure'));
            }
        }
        
        return;
    }
    
    public function actionSetFinished()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $item = Invest::model() -> findByPk($_POST['id']);
            
            $item -> isFinished = 1;
            
             if ($item -> save()) {
                echo json_encode(array("status" => 'success'));
            } else {
                echo json_encode(array("status" => 'failure'));
            }
        }
        
        return;
    }
    
    public function actionGetItem()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $id = $_POST['id'];
            $hyip = new Hyip();
            $item = $hyip -> getItem($id);
            
            echo json_encode($item);
        }
        
        return;
    }    
    
    public function actionGetTransaction()
    {
        if(Yii::app()->request->isPostRequest && !empty($_POST['id']) ){
            $id = $_POST['id'];
            $transaction = new Transaction();
            $data = $transaction -> getByInvest($id);
            
            echo json_encode(array("results" => $data[0], "total" => $data[1]));
            return;
        }
        
        echo json_encode(array("results" => array(), "total" => 0));
    }   
    
    public function actionDeleteTransaction()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $item = Transaction::model() -> findByPk($_POST['id']);
             if ($item -> delete()) {
                $invest = new Invest();
                $invest -> updateWithdraw($item -> investId);
                
                echo json_encode(array("status" => 'success'));
            } else {
                echo json_encode(array("status" => 'failure'));
            }
        }
        
        return;
    }
    
    public function actionCreateTransaction()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $item = new Transaction();
            $item -> investId = $_POST['investId'];
            $item -> withdraw = $_POST['withdraw'];
            $item -> fee = $_POST['fee'];
            $item -> isRCB = $_POST['isRCB'];

            $format = "Y-m-d H:i";
            if(!empty($_POST['transactionDate']))
            {
                $item -> createdDate = date($format, strtotime(str_replace("/", "-", $_POST['transactionDate'])));
            }
            
            if($item -> save())
            {
                $invest = new Invest();
                $invest -> updateWithdraw($item -> investId);
                
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
        
        return;
    }
    
    public function actionRefreshSummary()
    {
        $invest = new Invest();
        $summary = $invest ->getSummary();
        
        Yii::app()->session['summary'] = $summary;
        
        echo json_encode(array("status" => 'success')); 
    }
}